package ch08_Swing高级组件.list;

import javax.swing.*;
import java.awt.*;

public class CityListCellRenderer extends DefaultListCellRenderer {
	Color unselectedBg = new Color(0xFFFFFF);	//未选中项的背景色
	Color selectedBg = new Color(0x666666);		//已选中项的背景色
	Color unselectedFg = new Color(0x000000);	//未选中项的前景色
	Color selectedFg = new Color(0xFFFFFF);		//已选中项的前景色


	@Override//列表组件在显示其包含的每个列表项时都会调用此方法
	public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
												  boolean cellHasFocus) {
		JPanel item = (JPanel) value;					//得到当前列表项
		JLabel city = (JLabel) item.getComponent(1);	//得到当前项中的组件

		//根据当前项是否被选中，设置不同的背景、前景色
		item.setBackground(isSelected ?selectedBg:unselectedBg);
		city.setForeground(isSelected ?selectedFg:unselectedFg);
		return item;
	}
//	Public Component getListCellRendererComponent(JList ch08.list,Object value, int index,boolean isSelected,boolean cellHasFocus) {
//		JPanel item = (JPanel) value;
//		JLabel city = (JLabel) item.getComponent(1);
//		
//		item.setBackground(isSelected ?selectedBg:unselectedBg);
//		city.setForeground(isSelected ?selectedFg:unselectedFg);
//		return item;
//	}
}
